﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.Services;

namespace SLControlLibrary.Web
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class FileUpload : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            HttpServerUtility server = context.Server;

            if (!string.IsNullOrEmpty(context.Request.Params["filename"].ToString()))
            {
                using (Stream inputStream = request.InputStream)
                {
                    string folder = Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "ClientBin"));

                    using (FileStream fs = new FileStream(folder + @"\" + context.Request.Params["filename"], FileMode.OpenOrCreate))
                    {
                        byte[] fileContent = new byte[inputStream.Length];
                        inputStream.Read(fileContent, 0, fileContent.Length);
                        fs.Write(fileContent, 0, fileContent.Length);
                        fs.Flush();
                    }
                }
                response.Clear();
                response.End();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

}
